;;;; Created on 2007-03-24 22:17:28

;;; LIST-TOOLS

;; Randomly takes an element from a list
(defun random-elt (choices)
  (elt choices (random (length choices))))

;; Folds the lst into folds the size of n
(defun nfold (lst n)
  (let ((dlst lst) (folds (list )))
    (loop
      (when (= (length dlst) 0)
        (return folds))
      (setf folds (cons (list ) folds))
      (dotimes (i n)
        (when (= (length dlst) 0)
          (return folds))
        (setf (car folds) (cons (random-elt dlst) (car folds)))
        (setf dlst (remove (car (car folds)) dlst))))))


;; Folds the lst into folds the size of n and conses the fold with the not folds
(defun nfold-with-set (lst n)
  (let ((folds (nfold lst n)) (dlst nil))
    (progn
      (dotimes (i (length folds))
        (setf dlst lst)
        (dolist (val (nth i folds))
          (setf dlst (remove val dlst)))
        (setf (nth i folds) (cons (nth i folds) dlst)))
      folds)))

;; Folds the list into portions, so 10 portions, portion = 0.1
(defun portion-nfold (lst portion)
  (nfold lst (ceiling (* (length lst) portion))))

;; Folds the list into portions with not in set conses, so 10 portions, portion = 0.1
(defun portion-nfold-with-set (lst portion)
  (nfold-with-set lst (ceiling (* (length lst) portion))))

(defun invert-list (lst)
  (let ((r nil))
    (progn
      (dotimes (i (length (car lst)))
        (setf r (cons (list ) r)))
      (dolist (sub lst)
        (dotimes (i (length sub))
          (setf (nth i r) (cons (nth i sub) (nth i r)))))
      r)))
